home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / time-stamp.el.z / time-stamp.el
Encoding:
Text File  |  1998-05-21  |  18.3 KB  |  536 lines

  1. ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
  2.  
  3. ;; Copyright 1989, 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer's Time-stamp: <95/12/28 19:48:49 gildea>
  6. ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
  7. ;; Keywords: tools
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  22. ;; 02111-1307, USA.
  23.  
  24. ;;; Synched up with: 19.34.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; If you put a time stamp template anywhere in the first 8 lines of a file,
  29. ;; it can be updated every time you save the file.  See the top of
  30. ;; time-stamp.el for a sample.  The template looks like one of the following:
  31. ;;     Time-stamp: <>
  32. ;;     Time-stamp: " "
  33. ;; The time stamp is written between the brackets or quotes, resulting in
  34. ;;     Time-stamp: <95/01/18 10:20:51 gildea>
  35. ;; Here is an example that puts the file name and time stamp in the binary:
  36. ;; static char *time_stamp = "sdmain.c Time-stamp: <>";
  37.  
  38. ;; To activate automatic time stamping in GNU Emacs 19, add this code
  39. ;; to your .emacs file:
  40. ;; (add-hook 'write-file-hooks 'time-stamp)
  41. ;;
  42. ;; In Emacs 18 you will need to do this instead:
  43. ;; (if (not (memq 'time-stamp write-file-hooks))
  44. ;;     (setq write-file-hooks
  45. ;;           (cons 'time-stamp write-file-hooks)))
  46. ;; (autoload 'time-stamp "time-stamp" "Update the time stamp in a buffer." t)
  47.  
  48. ;; See the documentation for the function `time-stamp' for more details.
  49.  
  50. ;;; Change Log:
  51.  
  52. ;; Originally based on the 19 Dec 88 version of
  53. ;;   date.el by John Sturdy <mcvax!harlqn.co.uk!jcgs@uunet.uu.net>
  54. ;; version 2, January 1995: replaced functions with %-escapes
  55. ;; $Id: time-stamp.el,v 1.18 1996/01/20 02:49:02 erik Exp $
  56.  
  57. ;;; Code:
  58.  
  59. (defgroup time-stamp nil
  60.   "Maintain last change time stamps in files edited by Emacs."
  61.   :group 'data
  62.   :group 'extensions)
  63.  
  64.  
  65. (defcustom time-stamp-active t
  66.   "*Non-nil to enable time-stamping of buffers by \\[time-stamp].
  67. Can be toggled by \\[time-stamp-toggle-active].
  68. See also the variable time-stamp-warn-inactive."
  69.   :type 'boolean
  70.   :group 'time-stamp)
  71.  
  72. (defcustom time-stamp-warn-inactive t
  73.   "*Non-nil to have \\[time-stamp] warn if a buffer did not get time-stamped.
  74. A warning is printed if time-stamp-active is nil and the buffer contains
  75. a time stamp template that would otherwise have been updated."
  76.   :type 'boolean
  77.   :group 'time-stamp)
  78.  
  79. (defcustom time-stamp-format "%02y/%02m/%02d %02H:%02M:%02S %u"
  80.   "*Template for the string inserted by \\[time-stamp].
  81. Value may be a string or a list.  (Lists are supported only for
  82. backward compatibility.)  A string is used verbatim except
  83. for character sequences beginning with %:
  84.  
  85. %a  weekday name: `Monday'.        %A gives uppercase: `MONDAY'
  86. %b  month name: `January'.        %B gives uppercase: `JANUARY'
  87. %d  day of month
  88. %H  24-hour clock hour
  89. %I  12-hour clock hour
  90. %m  month number
  91. %M  minute
  92. %p  `am' or `pm'.            %P gives uppercase: `AM' or `PM'
  93. %S  seconds
  94. %w  day number of week, Sunday is 0
  95. %y  year: `1995'
  96. %z  time zone name: `est'.        %Z gives uppercase: `EST'
  97.  
  98. Non-date items:
  99. %%  a literal percent character: `%'
  100. %f  file name without directory        %F gives absolute pathname
  101. %s  system name
  102. %u  user's login name
  103. %h  mail host name
  104.  
  105. Decimal digits between the % and the type character specify the
  106. field width.  Strings are truncated on the right; numbers on the left.
  107. A leading zero causes numbers to be zero-filled.
  108.  
  109. For example, to get the format used by the `date' command,
  110. use \"%3a %3b %2d %02H:%02M:%02S %Z %y\""
  111.   :type 'string
  112.   :group 'time-stamp)
  113.  
  114.  
  115. ;;; Do not change time-stamp-line-limit, time-stamp-start, or
  116. ;;; time-stamp-end in your .emacs or you will be incompatible
  117. ;;; with other people's files!  If you must change them,
  118. ;;; do so only in the local variables section of the file itself.
  119.  
  120. (defvar time-stamp-line-limit 8        ;Do not change!
  121.   "Lines of a file searched; positive counts from start, negative from end.
  122. The patterns `time-stamp-start' and `time-stamp-end' must be found on one
  123. of the first (last) `time-stamp-line-limit' lines of the file for the
  124. file to be time-stamped by \\[time-stamp].
  125.  
  126. Do not change `time-stamp-line-limit', `time-stamp-start', or
  127. `time-stamp-end' for yourself or you will be incompatible
  128. with other people's files!  If you must change them for some application,
  129. do so in the local variables section of the time-stamped file itself.")
  130.  
  131.  
  132. (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+"    ;Do not change!
  133.   "Regexp after which the time stamp is written by \\[time-stamp].
  134. See also the variables `time-stamp-end' and `time-stamp-line-limit'.
  135.  
  136. Do not change `time-stamp-line-limit', `time-stamp-start', or
  137. `time-stamp-end' for yourself or you will be incompatible
  138. with other people's files!  If you must change them for some application,
  139. do so in the local variables section of the time-stamped file itself.")
  140.  
  141.  
  142. (defvar time-stamp-end "\\\\?[\">]"    ;Do not change!
  143.   "Regexp marking the text after the time stamp.
  144. \\[time-stamp] deletes the text between the first match of `time-stamp-start'
  145. and the following match of `time-stamp-end' on the same line,
  146. then writes the time stamp specified by `time-stamp-format' between them.
  147.  
  148. Do not change `time-stamp-line-limit', `time-stamp-start', or
  149. `time-stamp-end' for yourself or you will be incompatible
  150. with other people's files!  If you must change them for some application,
  151. do so in the local variables section of the time-stamped file itself.")
  152.  
  153.  
  154. ;;;###autoload
  155. (defun time-stamp ()
  156.   "Update the time stamp string in the buffer.
  157. If you put a time stamp template anywhere in the first 8 lines of a file,
  158. it can be updated every time you save the file.  See the top of
  159. `time-stamp.el' for a sample.  The template looks like one of the following:
  160.     Time-stamp: <>
  161.     Time-stamp: \" \"
  162. The time stamp is written between the brackets or quotes, resulting in
  163.     Time-stamp: <95/01/18 10:20:51 gildea>
  164. Only does its thing if the variable  time-stamp-active  is non-nil.
  165. Typically used on  write-file-hooks  for automatic time-stamping.
  166. The format of the time stamp is determined by the variable  time-stamp-format.
  167. The variables time-stamp-line-limit, time-stamp-start, and time-stamp-end
  168. control finding the template."
  169.   (interactive)
  170.   (let ((case-fold-search nil)
  171.     (need-to-warn nil)
  172.     start search-end)
  173.     (if (and (stringp time-stamp-start)
  174.          (stringp time-stamp-end))
  175.     (save-excursion
  176.       (save-restriction
  177.         (widen)
  178.         (if (> time-stamp-line-limit 0)
  179.         (progn
  180.           (goto-char (setq start (point-min)))
  181.           (forward-line time-stamp-line-limit)
  182.           (setq search-end (point)))
  183.           (goto-char (setq search-end (point-max)))
  184.           (forward-line time-stamp-line-limit)
  185.           (setq start (point)))
  186.         (goto-char start)
  187.         (while
  188.         (and (< (point) search-end)
  189.              (re-search-forward time-stamp-start search-end 'move))
  190.           (setq start (point))
  191.           (end-of-line)
  192.           (let ((line-end (point)))
  193.         (goto-char start)
  194.         (if (re-search-forward time-stamp-end line-end 'move)
  195.             (progn
  196.               (if time-stamp-active
  197.               (let ((end (match-beginning 0)))
  198.                 (delete-region start end)
  199.                 (goto-char start)
  200.                 (insert (time-stamp-string))
  201.                 (setq end (point))
  202.                 ;; remove any tabs used to format time stamp
  203.                 (goto-char start)
  204.                 (if (search-forward "\t" end t)
  205.                 (untabify start end)))
  206.             (if time-stamp-warn-inactive
  207.                 ;; do warning outside save-excursion
  208.                 (setq need-to-warn t)))
  209.               (setq search-end (point))))))))
  210.       ;; don't signal an error in a write-file-hook
  211.       (message "time-stamp-start or time-stamp-end is not a string")
  212.       (sit-for 1))
  213.     (if need-to-warn
  214.     (progn
  215.       (message "Warning: time-stamp-active is off; did not time-stamp buffer.")
  216.       (sit-for 1))))
  217.   ;; be sure to return nil so can be used on write-file-hooks
  218.   nil)
  219.  
  220. ;;;###autoload
  221. (defun time-stamp-toggle-active (&optional arg)
  222.   "Toggle time-stamp-active, setting whether \\[time-stamp] updates a buffer.
  223. With arg, turn time stamping on if and only if arg is positive."
  224.   (interactive "P")
  225.   (setq time-stamp-active
  226.     (if (null arg)
  227.         (not time-stamp-active)
  228.       (> (prefix-numeric-value arg) 0)))
  229.   (message "time-stamp is now %s." (if time-stamp-active "active" "off")))
  230.  
  231.  
  232. (defun time-stamp-string ()
  233.   "Generate the new string to be inserted by \\[time-stamp]."
  234.   (if (stringp time-stamp-format)
  235.       (time-stamp-strftime time-stamp-format)
  236.     (time-stamp-fconcat time-stamp-format " "))) ;version 1 compatibility
  237.  
  238. (defconst time-stamp-month-numbers
  239.   '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4) ("May" . 5) ("Jun" . 6)
  240.     ("Jul" . 7) ("Aug" . 8) ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
  241.   "Alist of months and their number.")
  242.  
  243. (defconst time-stamp-month-full-names
  244.   ["(zero)" "January" "February" "March" "April" "May" "June"
  245.    "July" "August" "September" "October" "November" "December"])
  246.  
  247. (defconst time-stamp-weekday-numbers
  248.   '(("Sun" . 0) ("Mon" . 1) ("Tue" . 2) ("Wed" . 3)
  249.     ("Thu" . 4) ("Fri" . 5) ("Sat" . 6))
  250.   "Alist of weekdays and their number.")
  251.  
  252. (defconst time-stamp-weekday-full-names
  253.   ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"])
  254.  
  255. (defconst time-stamp-am-pm '("am" "pm")
  256.   "List of strings used to denote morning and afternoon.")
  257.  
  258. (defconst time-stamp-no-file "(no file)"
  259.   "String to use when the buffer is not associated with a file.")
  260.  
  261. (defun time-stamp-strftime (format &optional time)
  262.   "Uses a FORMAT to format date, time, file, and user information.
  263. Optional second argument TIME will be used instead of the current time.
  264. See the description of the variable `time-stamp-format' for a description
  265. of the format string."
  266.   (let ((time-string (cond ((stringp time)
  267.                 time)
  268.                (time
  269.                 (current-time-string time))
  270.                (t
  271.                 (current-time-string))))
  272.     (fmt-len (length format))
  273.     (ind 0)
  274.     cur-char
  275.     (prev-char nil)
  276.     (result "")
  277.     field-index
  278.     field-width
  279.     field-result
  280.     (paren-level 0))
  281.     (while (< ind fmt-len)
  282.       (setq cur-char (aref format ind))
  283.       (setq
  284.        result
  285.        (concat result 
  286.       (cond
  287.        ((eq cur-char ?%)
  288.     (setq field-index (1+ ind))
  289.     (while (progn
  290.          (setq ind (1+ ind))
  291.          (setq cur-char (if (< ind fmt-len)
  292.                     (aref format ind)
  293.                   ?\0))
  294.          (and (<= ?0 cur-char) (>= ?9 cur-char))))
  295.     (setq field-width (substring format field-index ind))
  296.     ;; eat any additional args to allow for future expansion
  297.     (while (or (and (<= ?0 cur-char) (>= ?9 cur-char)) (eq ?. cur-char)
  298.            (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char)
  299.            (eq ?- cur-char) (eq ?+ cur-char)
  300.            (eq ?\  cur-char) (eq ?# cur-char)
  301.            (and (eq ?\( cur-char)
  302.             (not (eq prev-char ?\\))
  303.             (setq paren-level (1+ paren-level)))
  304.            (if (and (eq ?\) cur-char)
  305.                 (not (eq prev-char ?\\))
  306.                 (> paren-level 0))
  307.                (setq paren-level (1- paren-level))
  308.              (and (> paren-level 0)
  309.               (< ind fmt-len))))
  310.       (setq ind (1+ ind))
  311.       (setq prev-char cur-char)
  312.       (setq cur-char (if (< ind fmt-len)
  313.                  (aref format ind)
  314.                ?\0)))
  315.     (setq field-result
  316.     (cond
  317.      ((eq cur-char ?%)
  318.       "%")
  319.      ((or (eq cur-char ?a)        ;weekday name
  320.           (eq cur-char ?A))
  321.       (let ((name
  322.          (aref time-stamp-weekday-full-names
  323.                (cdr (assoc (substring time-string 0 3)
  324.                    time-stamp-weekday-numbers)))))
  325.         (if (eq cur-char ?a)
  326.         name
  327.           (upcase name))))
  328.      ((or (eq cur-char ?b)        ;month name
  329.           (eq cur-char ?B))
  330.       (let ((name
  331.          (aref time-stamp-month-full-names
  332.                (cdr (assoc (substring time-string 4 7)
  333.                    time-stamp-month-numbers)))))
  334.         (if (eq cur-char ?b)
  335.         name
  336.           (upcase name))))
  337.      ((eq cur-char ?d)        ;day of month, 1-31
  338.       (string-to-int (substring time-string 8 10)))
  339.      ((eq cur-char ?H)        ;hour, 0-23
  340.       (string-to-int (substring time-string 11 13)))
  341.      ((eq cur-char ?I)        ;hour, 1-12
  342.       (let ((hour (string-to-int (substring time-string 11 13))))
  343.         (cond ((< hour 1)
  344.            (+ hour 12))
  345.           ((> hour 12)
  346.            (- hour 12))
  347.           (t
  348.            hour))))
  349.      ((eq cur-char ?m)        ;month number, 1-12
  350.       (cdr (assoc (substring time-string 4 7)
  351.               time-stamp-month-numbers)))
  352.      ((eq cur-char ?M)        ;minute, 0-59
  353.       (string-to-int (substring time-string 14 16)))
  354.      ((or (eq cur-char ?p)        ;am or pm
  355.           (eq cur-char ?P))
  356.       (let ((name
  357.          (if (> 12 (string-to-int (substring time-string 11 13)))
  358.              (car time-stamp-am-pm)
  359.            (car (cdr time-stamp-am-pm)))))
  360.         (if (eq cur-char ?p)
  361.         name
  362.           (upcase name))))
  363.      ((eq cur-char ?S)        ;seconds, 00-60
  364.       (string-to-int (substring time-string 17 19)))
  365.      ((eq cur-char ?w)        ;weekday number, Sunday is 0
  366.       (cdr (assoc (substring time-string 0 3) time-stamp-weekday-numbers)))
  367.      ((eq cur-char ?y)        ;year
  368.       (string-to-int (substring time-string -4)))
  369.      ((or (eq cur-char ?z)        ;time zone
  370.           (eq cur-char ?Z))
  371.       (let ((name
  372.          (if (fboundp 'current-time-zone)
  373.              (car (cdr (current-time-zone time))))))
  374.         (or name (setq name ""))
  375.         (if (eq cur-char ?z)
  376.         (downcase name)
  377.           (upcase name))))
  378.      ((eq cur-char ?f)        ;buffer-file-name, base name only
  379.       (if buffer-file-name
  380.           (file-name-nondirectory buffer-file-name)
  381.         time-stamp-no-file))
  382.      ((eq cur-char ?F)        ;buffer-file-name, full path
  383.       (or buffer-file-name
  384.           time-stamp-no-file))
  385.      ((eq cur-char ?s)        ;system name
  386.       (system-name))
  387.      ((eq cur-char ?u)        ;user name
  388.       (user-login-name))
  389.      ((eq cur-char ?h)        ;mail host name
  390.       (time-stamp-mail-host-name))
  391.      ))
  392.     (if (string-equal field-width "")
  393.         field-result
  394.       (let ((padded-result
  395.          (format (format "%%%s%c"
  396.                  field-width
  397.                  (if (numberp field-result) ?d ?s))
  398.              (or field-result ""))))
  399.         (let ((initial-length (length padded-result))
  400.           (desired-length (string-to-int field-width)))
  401.           (if (> initial-length desired-length)
  402.           ;; truncate strings on right, numbers on left
  403.           (if (stringp field-result)
  404.               (substring padded-result 0 desired-length)
  405.             (substring padded-result (- desired-length)))
  406.         padded-result)))))
  407.        (t
  408.     (char-to-string cur-char)))))
  409.       (setq ind (1+ ind)))
  410.     result))
  411.  
  412. (defun time-stamp-mail-host-name ()
  413.   "Return the name of the host where the user receives mail.
  414. This is the value of `mail-host-address' if bound and a string,
  415. otherwise the value of `time-stamp-mail-host' (for versions of Emacs
  416. before 19.29) otherwise the value of the function system-name."
  417.   (or (and (boundp 'mail-host-address)
  418.        (stringp mail-host-address)
  419.        mail-host-address)
  420.       (and (boundp 'time-stamp-mail-host) ;for backward compatibility
  421.        (stringp time-stamp-mail-host)
  422.        time-stamp-mail-host)
  423.       (system-name)))
  424.  
  425. ;;; the rest of this file is for version 1 compatibility
  426.  
  427. (defun time-stamp-fconcat (list sep)
  428.   "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
  429. If an element of LIST is a symbol, it is funcalled to get the string to use;
  430. the separator SEP is used between two strings obtained by funcalling a
  431. symbol.  Otherwise the element itself is inserted; no separator is used
  432. around literals."
  433.   (let ((return-string "")
  434.     (insert-sep-p nil))
  435.     (while list
  436.       (cond ((symbolp (car list))
  437.          (if insert-sep-p
  438.          (setq return-string (concat return-string sep)))
  439.          (setq return-string (concat return-string (funcall (car list))))
  440.          (setq insert-sep-p t))
  441.         (t
  442.          (setq return-string (concat return-string (car list)))
  443.          (setq insert-sep-p nil)))
  444.       (setq list (cdr list)))
  445.     return-string))
  446.  
  447.  
  448. ;;; Some useful functions to use in time-stamp-format
  449.  
  450. ;;; Could generate most of a message-id with
  451. ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name)
  452.  
  453. ;;; pretty form, suitable for a title page
  454.  
  455. (defun time-stamp-month-dd-yyyy ()
  456.   "Return the current date as a string in \"Month DD, YYYY\" form."
  457.   (let ((date (current-time-string)))
  458.     (format "%s %d, %s"
  459.         (aref time-stamp-month-full-names
  460.           (cdr (assoc (substring date 4 7) time-stamp-month-numbers)))
  461.         (string-to-int (substring date 8 10))
  462.         (substring date -4))))
  463.  
  464. ;;; same as __DATE__ in ANSI C
  465.  
  466. (defun time-stamp-mon-dd-yyyy ()
  467.   "Return the current date as a string in \"Mon DD YYYY\" form.
  468. The first character of DD is space if the value is less than 10."
  469.   (let ((date (current-time-string)))
  470.     (format "%s %2d %s"
  471.         (substring date 4 7)
  472.         (string-to-int (substring date 8 10))
  473.         (substring date -4))))
  474.  
  475. ;;; RFC 822 date
  476.  
  477. (defun time-stamp-dd-mon-yy ()
  478.   "Return the current date as a string in \"DD Mon YY\" form."
  479.   (let ((date (current-time-string)))
  480.     (format "%02d %s %s"
  481.         (string-to-int (substring date 8 10))
  482.         (substring date 4 7)
  483.         (substring date -2))))
  484.  
  485. ;;; RCS 3 date
  486.  
  487. (defun time-stamp-yy/mm/dd ()
  488.   "Return the current date as a string in \"YY/MM/DD\" form."
  489.   (let ((date (current-time-string)))
  490.     (format "%s/%02d/%02d"
  491.         (substring date -2)
  492.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  493.         (string-to-int (substring date 8 10)))))
  494.  
  495. ;;; RCS 5 date
  496.  
  497. (defun time-stamp-yyyy/mm/dd ()
  498.   "Return the current date as a string in \"YYYY/MM/DD\" form."
  499.   (let ((date (current-time-string)))
  500.     (format "%s/%02d/%02d"
  501.         (substring date -4)
  502.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  503.         (string-to-int (substring date 8 10)))))
  504.  
  505. ;;; ISO 8601 date
  506.  
  507. (defun time-stamp-yyyy-mm-dd ()
  508.   "Return the current date as a string in \"YYYY-MM-DD\" form."
  509.   (let ((date (current-time-string)))
  510.     (format "%s-%02d-%02d"
  511.         (substring date -4)
  512.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  513.         (string-to-int (substring date 8 10)))))
  514.  
  515. (defun time-stamp-yymmdd ()
  516.   "Return the current date as a string in \"YYMMDD\" form."
  517.   (let ((date (current-time-string)))
  518.     (format "%s%02d%02d"
  519.         (substring date -2)
  520.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  521.         (string-to-int (substring date 8 10)))))
  522.  
  523. (defun time-stamp-hh:mm:ss ()
  524.   "Return the current time as a string in \"HH:MM:SS\" form."
  525.   (substring (current-time-string) 11 19))
  526.  
  527. (defun time-stamp-hhmm ()
  528.   "Return the current time as a string in \"HHMM\" form."
  529.   (let ((date (current-time-string)))
  530.     (concat (substring date 11 13)
  531.         (substring date 14 16))))
  532.  
  533. (provide 'time-stamp)
  534.  
  535. ;;; time-stamp.el ends here
  536.